package csci5115.autoalarm;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

public class CalendarViewAdapter extends BaseAdapter implements OnClickListener {

	private final Context mCtx;
	private final List<String> list;

	private final int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
			30, 31 };
	int daysInMonth, prevMonthDays;
	private int currentDayOfMonth;
	private Button gridcell;

	// Days in Current Month
	public CalendarViewAdapter(Context context, int textViewResourceId,
			int month, int year) {
		super();
		mCtx = context;
		this.list = new ArrayList<String>();
		Calendar calendar = Calendar.getInstance();
		if (calendar.get(Calendar.MONTH) == month
				&& calendar.get(Calendar.YEAR) == year) {
			currentDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
		} else {
			currentDayOfMonth = -100;
		}
		printMonth(month, year);
	}

	public String getItem(int position) {
		return list.get(position);
	}

	public int getCount() {
		return list.size();
	}

	private void printMonth(int mm, int yy) {
		// The number of days to leave blank at
		// the start of this month.
		int trailingSpaces = 0;
		int daysInPrevMonth = 0;
		int prevMonth = 0;
		int prevYear = 0;
		int nextMonth = 0;
		int nextYear = 0;
		int day = 0;
		GregorianCalendar cal = new GregorianCalendar(yy, mm, 1);

		// Days in Current Month
		daysInMonth = daysOfMonth[mm];
		int currentMonth = mm;
		if (currentMonth == 11) {
			prevMonth = 10;
			daysInPrevMonth = daysOfMonth[prevMonth];
			nextMonth = 0;
			prevYear = yy;
			nextYear = yy + 1;
		} else if (currentMonth == 0) {
			prevMonth = 11;
			prevYear = yy - 1;
			nextYear = yy;
			daysInPrevMonth = daysOfMonth[prevMonth];
			nextMonth = 1;
		} else {
			prevMonth = currentMonth - 1;
			nextMonth = currentMonth + 1;
			nextYear = yy;
			prevYear = yy;
			daysInPrevMonth = daysOfMonth[prevMonth];
		}

		// Compute how much to leave before before the first day of the
		// month.
		// getDay() returns 0 for Sunday.
		trailingSpaces = cal.get(Calendar.DAY_OF_WEEK) - 1;

		if (cal.isLeapYear(cal.get(Calendar.YEAR)) && mm == 1) {
			++daysInMonth;
		}

		// Trailing Month days
		for (int i = 0; i < trailingSpaces; i++) {
			day = daysInPrevMonth - trailingSpaces + 1 + i;
			list.add(day + "-GREY" + "-" + prevMonth + "-" + prevYear);
			currentDayOfMonth++;
		}
		currentDayOfMonth--;
		// Current Month Days
		for (int i = 1; i <= daysInMonth; i++) {
			list.add(i + "-WHITE" + "-" + mm + "-" + yy);
		}

		// Leading Month days
		for (int i = 0; i < list.size() % 7; i++) {
			day = i + 1;
			list.add(day + "-GREY" + "-" + nextMonth + "-" + nextYear);
		}
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			// ROW INFLATION
			LayoutInflater inflater = (LayoutInflater) mCtx
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.day_gridcell, parent, false);

		} 

		// Get a reference to the Day gridcell
		gridcell = (Button) row.findViewById(R.id.day_gridcell);
		gridcell.setOnClickListener(this);

		// ACCOUNT FOR SPACING

		String[] day_color = list.get(position).split("-");
		gridcell.setText(day_color[0]);
		gridcell.setTag(day_color[0] + "-" + day_color[2] + "-" + day_color[3]);

		if (day_color[1].equals("GREY")) {
			gridcell.setTextColor(Color.DKGRAY);
		}
		if (day_color[1].equals("WHITE")) {
			gridcell.setTextColor(Color.WHITE);
		}
		if (position == currentDayOfMonth) {
			gridcell.setTextColor(Color.GREEN);
		}

		return row;
	}

	public void onClick(View view) {
		String tag = (String) view.getTag();
		String[] daymonthyear = tag.split("-");
		String day = daymonthyear[0];
		String month = daymonthyear[1];
		String year = daymonthyear[2];
		int d = Integer.parseInt(day);
		int y = Integer.parseInt(year);
		int m = Integer.parseInt(month);
		NewDayView.curDate.setYear(y - 1900);
		NewDayView.curDate.setMonth(m);
		NewDayView.curDate.setDate(d);
		AutoAlarmActivity.tabHost.setCurrentTab(1);

	}
}